﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFMINERP.MODEL;
using System.Xml;
using System.IO;
using WCFMINERP.COM;

namespace WCFMINERP.Order.Presenter
{
    public class OrderPesenter
    {
        

        /// <summary>
        /// 根据内存中的数据组织XML并保存
        /// </summary>
        /// <param name="inOrderBag"></param>
        /// <param name="savePath"></param>
        public void ProductXml(OrderBag inOrderBag, string savePath)
        {

            XmlDocument newBagXml = new XmlDocument();
            XmlDeclaration xd = newBagXml.CreateXmlDeclaration("1.0", "utf-8", null);
            newBagXml.AppendChild(xd);

            XmlProcessingInstruction xpi = newBagXml.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"orderbag.xsl\"");
            newBagXml.AppendChild(xpi);
            //XmlNode xn = newBagXml.CreateNode(XmlNodeType.Element, "orderbag", null);

            //XmlAttribute xa = newBagXml.CreateAttribute("OrderbagID");
            //xa.Value = "";
            XmlElement xeOrderBag = newBagXml.CreateElement("orderbag");//orderbag元素
            xeOrderBag.SetAttribute("OrderbagID", inOrderBag.OrderbagID.ToString());
            xeOrderBag.SetAttribute("SerialNum", inOrderBag.SerialNum.ToString());
            xeOrderBag.SetAttribute("OrderbagName", inOrderBag.OrderbagName);
            xeOrderBag.SetAttribute("RARName", inOrderBag.RARName);
            xeOrderBag.SetAttribute("ShopId", inOrderBag.ShopId.ToString());
            xeOrderBag.SetAttribute("OrderTime", inOrderBag.OrderTime.ToString("yyyy-MM-dd HH:mm:ss"));
            xeOrderBag.SetAttribute("FetchTime", inOrderBag.FetchTime.ToString("yyyy-MM-dd HH:mm:ss"));
            xeOrderBag.SetAttribute("Status", inOrderBag.Status.ToString());
            xeOrderBag.SetAttribute("Priority", inOrderBag.Priority.ToString());
            xeOrderBag.SetAttribute("PrePriceCount", inOrderBag.PrePriceCount.ToString("0.00"));
            xeOrderBag.SetAttribute("Remark", inOrderBag.Remark);

            newBagXml.AppendChild(xeOrderBag);

            //List<OrderInfo> orderList = inOrderBag.OrderInfoList;
            foreach (OrderInfo order in inOrderBag.OrderInfoList)
            {
                ///产品元素
                XmlElement xeorder = newBagXml.CreateElement("orderinfo");
                xeorder.SetAttribute("OrderProductId", order.OrderProductId.ToString());
                xeorder.SetAttribute("OrderProductId", order.OrderProductId.ToString());
                xeorder.SetAttribute("OrderProductName", order.OrderProductName.ToString());
                xeorder.SetAttribute("ProductId", order.ProductId.ToString());
                xeorder.SetAttribute("CtmName", order.CtmName.ToString());
                xeorder.SetAttribute("Status", order.Status.ToString());
                xeorder.SetAttribute("Width", order.Width.ToString());
                xeorder.SetAttribute("Height", order.Height.ToString());
                xeorder.SetAttribute("productNum", order.productNum.ToString());
                xeorder.SetAttribute("PageNum", order.PageNum.ToString());
                xeorder.SetAttribute("PrePriceCount", order.PrePriceCount.ToString());
                xeorder.SetAttribute("Priority", order.Priority.ToString());
                xeorder.SetAttribute("Remark", order.Remark.ToString());
                xeOrderBag.AppendChild(xeorder);
                foreach (PhotoInfo photo in order.PhotoList)
                {
                    ///照片元素
                    XmlElement xePhoto = newBagXml.CreateElement("photoInfo");
                    xePhoto.SetAttribute("Photoid", photo.Photoid.ToString());
                    xePhoto.SetAttribute("photoName", photo.PhotoName.ToString());
                    xePhoto.SetAttribute("picPath", photo.PicPath.ToString());
                    xePhoto.SetAttribute("OrilPicPath", photo.OrilPicPath.ToString());
                    xePhoto.SetAttribute("Width", photo.Width.ToString());
                    xePhoto.SetAttribute("Height", photo.Height.ToString());
                    xePhoto.SetAttribute("pixWidth", photo.PixWidth.ToString());
                    xePhoto.SetAttribute("Pixheight", photo.PixHeight.ToString());
                    xePhoto.SetAttribute("paperTypeID", photo.PaperTypeID.ToString());
                    xePhoto.SetAttribute("paperFaceId", photo.PaperFaceId.ToString());
                    xePhoto.SetAttribute("TotalNum", photo.TotalNum.ToString());
                    xePhoto.SetAttribute("Photoid", photo.Photoid.ToString());
                    xePhoto.SetAttribute("Photoid", photo.Photoid.ToString());
                    xeorder.AppendChild(xePhoto);
                }

            }
            newBagXml.Save(savePath + "\\" + inOrderBag.ShopId + "" + DateTime.Now.ToString("yyyy-MM-dd") + inOrderBag.SerialNum);
            






        }
        /// <summary>
        /// 根据文件包拷贝文件并准备压缩
        /// </summary>
        /// <param name="inOrderBag"></param>
        public void CopyFile(OrderBag inOrderBag, string inWorkFolder)
        {
            foreach (OrderInfo order in inOrderBag.OrderInfoList)
            {
                string strOrderPath = inWorkFolder + "\\" + order.OrderPath;//产品路径
                if (!Directory.Exists(strOrderPath))
                {
                    Directory.CreateDirectory(strOrderPath);
                }
                foreach (PhotoInfo photo in order.PhotoList)
                {
                    int subIndex = photo.PicPath.ToLower().LastIndexOf(".jpg");
                    if (subIndex == 0)
                    {
                        subIndex = photo.PicPath.ToLower().LastIndexOf(".jpeg");
                    }

                    string photoDirpath = photo.PicPath.Substring(0, subIndex);
                    if (!Directory.Exists(photoDirpath))
                    {
                        Directory.CreateDirectory(photoDirpath);
                    }
                    if (!File.Exists(photo.OrilPicPath))
                    {
                        ///文件不存在报错处理
                        throw new Exception();
                    }
                    //复制文件
                    if (FileOperate.CopyFile(photo.OrilPicPath, strOrderPath + "\\" + photo.PicPath, true))
                    {

                    }
                    else
                    {
                        ///文件不存在报错处理
                        throw new Exception();
                    }

                }

            }


        }
    }
}
